package com.cyl.manager.yee.controller;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.redis.RedisService;
import com.ruoyi.common.enums.BusinessType;
import com.cyl.job.StatisticsJob;
import com.cyl.manager.yee.convert.MerchantStatisticsConvert;
import com.cyl.manager.yee.domain.MerchantStatistics;
import com.cyl.manager.yee.pojo.query.MerchantStatisticsQuery;
import com.cyl.manager.yee.service.MerchantStatisticsService;
import com.cyl.manager.yee.pojo.vo.MerchantStatisticsVO;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
/**
 * 费用统计Controller
 *
 * @author zcc
 * @date 2025-03-14
 */
@Api(description ="费用统计接口列表")
@RestController
@RequestMapping("/yee/merchantStatistics")
@Slf4j
public class MerchantStatisticsController extends BaseController {
    @Autowired
    private MerchantStatisticsService service;
    @Autowired
    private MerchantStatisticsConvert convert;
    
    @Autowired
    private RedisService redisService;

    @ApiOperation("查询费用统计列表")
    @PreAuthorize("@ss.hasPermi('yee:merchantStatistics:list')")
    @PostMapping("/list")
    public ResponseEntity<Page<MerchantStatistics>> list(@RequestBody MerchantStatisticsQuery query, Pageable page) {
    	Long qrDeptZbjId = SecurityUtils.getDeptZbjId();
        if(qrDeptZbjId!=null) {
        	query.setDeptZbjId(qrDeptZbjId);
        }
        List<MerchantStatistics> list = service.selectList(query, page);
        return ResponseEntity.ok(new PageImpl<>(list, page, ((com.github.pagehelper.Page)list).getTotal()));
    }
    
    
    @ApiOperation("查询费用统计列表")
    @PreAuthorize("@ss.hasPermi('yee:merchantStatistics:list')")
    @PostMapping("/getMerchantStatistics")
    public ResponseEntity<MerchantStatistics> getMerchantStatistics(@RequestBody MerchantStatisticsQuery query) {
    	Long qrDeptZbjId = SecurityUtils.getDeptZbjId();
        if(qrDeptZbjId!=null) {
        	query.setDeptZbjId(qrDeptZbjId);
        }
        MerchantStatistics merchantStatistics = service.getMerchantStatistics(query);
        return ResponseEntity.ok(merchantStatistics);
    }
    
    
    @ApiOperation("查询费用统计列表")
    @PreAuthorize("@ss.hasPermi('yee:merchantStatistics:list')")
    @PostMapping("/getMerchantStatisticsToDay")
    public ResponseEntity<MerchantStatistics> getMerchantStatisticsToDay() {
    	MerchantStatisticsQuery query = new MerchantStatisticsQuery();
    	Long qrDeptZbjId = SecurityUtils.getDeptZbjId();
        if(qrDeptZbjId!=null) {
        	query.setDeptZbjId(qrDeptZbjId);
        }
        MerchantStatistics merchantStatistics = service.getMerchantStatisticsToDay(query);
        return ResponseEntity.ok(merchantStatistics);
    }
    
    
    @ApiOperation("查询今日费用统计列表")
    @PreAuthorize("@ss.hasPermi('yee:merchantStatistics:list')")
    @PostMapping("/listToDay")
    public ResponseEntity<List<MerchantStatistics>> listToDay(@RequestBody MerchantStatisticsQuery query, Pageable page) {
    	Long qrDeptZbjId = SecurityUtils.getDeptZbjId();
        if(qrDeptZbjId!=null) {
        	query.setDeptZbjId(qrDeptZbjId);
        }
        return ResponseEntity.ok(service.selectByEntityToDay(query));
    }
    
    

    @ApiOperation("导出费用统计列表")
    @PreAuthorize("@ss.hasPermi('yee:merchantStatistics:export')")
    @Log(title = "费用统计", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public ResponseEntity<String> export(MerchantStatisticsQuery query) {
    	Long qrDeptZbjId = SecurityUtils.getDeptZbjId();
        if(qrDeptZbjId!=null) {
        	query.setDeptZbjId(qrDeptZbjId);
        }
        List<MerchantStatistics> list = service.selectList(query, null);
        ExcelUtil<MerchantStatisticsVO> util = new ExcelUtil<>(MerchantStatisticsVO.class);
        return ResponseEntity.ok(util.writeExcel(convert.dos2vos(list), "费用统计数据"));
    }

    @ApiOperation("获取费用统计详细信息")
    @PreAuthorize("@ss.hasPermi('yee:merchantStatistics:query')")
    @GetMapping(value = "/{id}")
    public ResponseEntity<MerchantStatistics> getInfo(@PathVariable("id") Long id) {
        return ResponseEntity.ok(service.selectById(id));
    }
 
    
    @ApiOperation("手工跟新数据")
    @GetMapping(value = "/insertMerchantSif")
    public ResponseEntity<String> insertMerchantSif(String date) {
        log.info("【统计昨日系统结算数据任务开始】");
        String ok = "OK";
    	LocalDate thisLocalDate = LocalDate.now();
    	if(date!=null) {
    		thisLocalDate = LocalDate.parse(date);
    	}
        LocalDateTime strTime = LocalDateTime.of(thisLocalDate, LocalTime.MIN).plusDays(-1);
    	LocalDateTime endTime = LocalDateTime.of(thisLocalDate, LocalTime.MAX).plusDays(-1);
    	String redisKey = "admin_yee_mer_sif_123456";
	    String redisValue = redisKey+"_"+strTime.toString();
	    try{    
			  redisService.lock(redisKey,redisValue,60*30);
			  service.insertMerchantStatisticsDay(strTime, endTime);
	        log.info("【统计昨日系统结算数据任务结束】");
	    	}catch (Exception e){
	    		log.error("统计昨日系统结算数据任务异常",e);
       		 	ok = "1: "+e.getMessage();
	    	}finally {
		        try {
	            redisService.unLock(redisKey,redisValue);
	        }catch (Exception e){
	            log.error("",e);
       		 	ok = "2: "+e.getMessage();
	        }
	    }
		return ResponseEntity.ok(ok);

    }
    
    
}
